﻿using Stock.Model.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stock.Dao
{
      public class TencentDao : BaseDao
      {
            #region T_HOLIDAY

            /// <summary>
            /// 获取指定月份的交易日数
            /// 1:工作日,3:双休日,4:节日
            /// </summary>
            /// <param name="year"></param>
            /// <param name="month"></param>
            /// <returns></returns>
            public static int GetHolidays(int year, int month, string status = "1")
            {
                  string sql = string.Format("SELECT * FROM T_HOLIDAY WHERE HOLIDAY_YEAR = '{0}'  AND HOLIDAY_MONTH = '{1}' AND STATUS = '{2}';", year, month, status);
                  return BaseDao.GetDataTable(sql).Rows.Count;
            }

            public static IList<THoliday> GetHolidayList(int year, int month = 0, string status = "1")
            {
                  string sql = string.Format("SELECT * FROM T_HOLIDAY WHERE  HOLIDAY_YEAR = '{0}'  AND  STATUS = '{1}' ", year, status);
                  if (month != 0)
                  {
                        sql += string.Format(" AND HOLIDAY_MONTH = '{0}' ;", month);
                  }
                  return BaseDao.GetList<THoliday>(sql);
            }

            #endregion


            #region TStock


            public static TStock GetStockByID(string id)
            {
                  string sql = string.Format("SELECT * FROM [T_STOCK] WHERE [id] = {0} ;", id);
                  TStock stock = GetObject<TStock>(sql);
                  return stock;
            }

            public static TStock GetStockByCode(string code)
            {
                  string sql = string.Format("SELECT * FROM [T_STOCK] WHERE [CODE] = '{0}' ;", code);
                  TStock stock = GetObject<TStock>(sql);
                  return stock;
            }



            /// <summary>
            /// 获取需要监控的STOCK列表
            /// </summary>
            /// <returns></returns>
            public static IList<TStock> GetStockList()
            {
                  string sql = "SELECT * FROM [T_STOCK] WHERE [IS_ALERT] = TRUE;";

                  IList<TStock> stocks = GetList<TStock>(sql);
                  return stocks;
            }

            public static TStockLog GetCurrentStockPrice(string code)
            {
                  string sql = string.Format("SELECT TOP 1 * FROM [T_STOCK_LOG] WHERE [CODE] = '{0}' ORDER BY ID DESC;", code);
                  TStockLog stockLog = GetObject<TStockLog>(sql);
                  return stockLog;
            }


            /// <summary>
            /// 更新
            /// </summary>
            /// <param name="id"></param>
            /// <param name="isTrading"></param>
            /// <param name="isAlert"></param>
            /// <param name="isWXAlert"></param>
            /// <returns></returns>
            public static bool UpdateStock(int id, bool isTrading, bool isAlert, bool isWXAlert, string remark)
            {
                  string sql = string.Format("UPDATE [T_STOCK] SET [IS_TRADING]={1},[IS_ALERT]={2},[IS_WEIXIN_ALERT]={3},[REMARKS]={4} WHERE [ID] = {0} ;",
                        id, isTrading, isAlert, isWXAlert, remark);
                  return BaseDao.Update(sql);
            }


            /// <summary>
            /// 删除
            /// </summary>
            /// <param name="id"></param>
            /// <param name="isTrading"></param>
            /// <param name="isAlert"></param>
            /// <param name="isWXAlert"></param>
            /// <returns></returns>
            public static bool DelStock(int id)
            {
                  string sql = string.Format("DELETE FROM [T_STOCK]  WHERE [ID] = {0} ;", id);
                  return BaseDao.Delete(sql);
            }


            #endregion

            #region TStockLog

            /// <summary>
            /// 清空分时价格
            /// </summary>
            public static void ClearStockLog()
            {
                  string sql = "DELETE FROM T_STOCK_LOG WHERE GET_DATE <= #"+DateTime.Now.ToShortDateString()+"#;";
                  BaseDao.Delete(sql);
            }

            #endregion

            #region TStockHistory

            public static bool DelTStockHistory(string code, DateTime dateTime)
            {
                  string sql = string.Format("DELETE FROM T_STOCK_HISTORY WHERE CODE='{0}' AND TRADING_DATE=#{1}#;", code, dateTime.ToString("yyyy-MM-dd"));
                  return BaseDao.Delete(sql);
            }

            public static bool InsertTStockHistory(IList<TStockHistory> stockHistorys)
            {
                  foreach (TStockHistory stockHistory in stockHistorys)
                  {
                        DelTStockHistory(stockHistory.CODE, stockHistory.TRADING_DATE);
                        BaseDao.Insert<TStockHistory>(stockHistory);
                  }
                  return true;
            }


            #endregion

            #region 保存形态

            #region 通道模型
            public static bool InsertChannels(IList<TStockChannel> stockChannels)
            {
                  foreach (TStockChannel stockChannel in stockChannels)
                        Insert<TStockChannel>(stockChannel); ;

                  return true;
            }

            public static bool InsertChannel(TStockChannel stockChannel)
            {
                  return Insert<TStockChannel>(stockChannel);
            }

            public static bool DelChannel(string code)
            {
                  string sql = string.Format("Delete FROM [T_STOCK_CHANNEL]  WHERE [CODE] = {0} ;", code);
                  return BaseDao.Delete(sql);
            }

            #endregion


            #region 箱体模型
            public static bool InsertEnclosures(IList<TStockEnclosure> stockEnclosures)
            {
                  foreach (TStockEnclosure stockEnclosure in stockEnclosures)
                        InsertEnclosure(stockEnclosure);
                  return true;
            }


            public static bool InsertEnclosure(TStockEnclosure stockEnclosure)
            {
                  return Insert<TStockEnclosure>(stockEnclosure);
            }

            public static bool DelEnclosure(string code)
            {
                  string sql = string.Format("Delete FROM [T_STOCK_ENCLOSURE]  WHERE [CODE] = {0} ;", code);
                  return BaseDao.Delete(sql);
            }


            #endregion

            #region 三角模型

            public static bool InsertTriangles(IList<TStockTriangle> stockTriangles)
            {
                  foreach (TStockTriangle stockTriangle in stockTriangles)
                        InsertTriangle(stockTriangle);
                  return true;
            }


            public static bool InsertTriangle(TStockTriangle stockTriangle)
            {
                  return Insert<TStockTriangle>(stockTriangle);
            }

            public static bool DelTriangle(string code)
            {
                  string sql = string.Format("Delete FROM [T_STOCK_TRIANGLE]  WHERE [CODE] = {0} ;", code);
                  return BaseDao.Delete(sql);
            }



            #endregion



            #endregion


            #region 交易计划


            /// <summary>
            /// 获取今日的操作计划
            /// </summary>
            /// <returns></returns>
            public static IList<TStockSchedule> GetStockSchedule(string code = "")
            {
                  string sql = string.Format("SELECT * FROM [T_STOCK_SCHEDULE] WHERE [SCHEDULE_DATE] >= #{0}# AND [SCHEDULE_DATE] <= #{0} 23:59:59# AND [IS_REAL_TRADING] = TRUE", DateTime.Now.ToString("yyyy-MM-dd"));
                  if (!string.IsNullOrEmpty(code))
                  {
                        sql += string.Format(" AND [CODE] = '{0}'", code);
                  }
                  IList<TStockSchedule> stockSchedules = TencentDao.GetList<TStockSchedule>(sql);
                  return stockSchedules;
            }

            /// <summary>
            /// 交易计划设置为已提醒
            /// </summary>
            /// <param name="id"></param>
            public static void UpdateStockSchedule(int id)
            {
                  string sql = string.Format("UPDATE [T_STOCK_SCHEDULE] SET  [IS_REAL_TRADING] = FALSE WHERE [ID] = {0}", id);
                  TencentDao.Update(sql);
            }

            /// <summary>
            /// 通道模型
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public static IList<TStockChannel> GetStockChannels(string code = "")
            {
                  string sql = "SELECT * FROM [T_STOCK_CHANNEL] ";
                  if (string.IsNullOrEmpty(code))
                        sql += ";";
                  else
                        sql += string.Format("WHERE [CODE]='{0}';", code);

                  return BaseDao.GetList<TStockChannel>(sql);
            }

            /// <summary>
            /// 箱体模型
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public static IList<TStockEnclosure> GetStockEnclosure(string code = "")
            {
                  string sql = "SELECT * FROM [T_STOCK_ENCLOSURE] ";
                  if (string.IsNullOrEmpty(code))
                        sql += ";";
                  else
                        sql += string.Format("WHERE [CODE]='{0}';", code);

                  return BaseDao.GetList<TStockEnclosure>(sql);
            }


            /// <summary>
            /// 三角模型
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public static IList<TStockTriangle> GetStockTriangle(string code = "")
            {
                  string sql = "SELECT * FROM [T_STOCK_TRIANGLE] ";
                  if (string.IsNullOrEmpty(code))
                        sql += ";";
                  else
                        sql += string.Format("WHERE [CODE]='{0}';", code);

                  return BaseDao.GetList<TStockTriangle>(sql);
            }

            /// <summary>
            /// 保存交易计划
            /// </summary>
            /// <param name="stockSchedules"></param>
            public static void SaveSchedule(IList<TStockSchedule> stockSchedules)
            {
                  if (stockSchedules == null || stockSchedules.Count == 0)
                        return;
                  foreach (TStockSchedule stockSchedule in stockSchedules)
                  {
                        DelSchedule(stockSchedule.CODE);
                  }                      
                  foreach (TStockSchedule stockSchedule in stockSchedules)
                  {           
                        Insert<TStockSchedule>(stockSchedule);
                  }
            }

            public static void DelSchedule(string code)
            {
                  string sql = string.Format("DELETE FROM [T_STOCK_SCHEDULE]  WHERE [CODE] = '{0}' AND [SCHEDULE_DATE] >= #{1}# AND [SCHEDULE_DATE] <= #{1} 23:59:59# ;", code, DateTime.Now.ToString("yyyy-MM-dd"));
                  BaseDao.Delete(sql);
            }

            #endregion

      }
}
